<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 搜索区域 -->
          <el-input v-model="queryInfo.query" placeholder="请输入内容" class="input-with-select" clearable
            @clear="getGoodList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAdd">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList" style="width: 100%" border>
        <!-- <el-table-column type="index" label="序号" /> -->
        <el-table-column prop="goods_id" label="商品ID" width="180" />
        <el-table-column prop="goods_name" label="商品名称" />
        <el-table-column prop="goods_price" label="商品价格（元）" />
        <el-table-column prop="goods_weight" label="商品重量" />
        <el-table-column prop="goods_number" label="商品数量" />
        <el-table-column prop="add_time" label="商品上架时间">
          <template slot-scope="scope">
            {{scope.row.add_time|dataFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" type="primary" size="mini" />
            <el-button icon=" el-icon-delete" type="danger" size="mini" @click="removeGoodById(scope.row.goods_id)" />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  created () {
    this.getGoodList()
  },
  data () {
    return {
      goodsList: [],
      //获取商品列表的参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0
    }
  },
  methods: {
    async getGoodList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        this.$message.error('数据请求失败，请检查网络')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 监听显示数据条数
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodList()
    },
    // 监听页码值改变的事件(翻页)
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodList()

    },
    //删除商品
    async removeGoodById (goods_id) {
      const result = await this.$confirm('此操作将删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(result)
      if (result !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete('goods/' + goods_id)
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error('请检查网络')
      this.$message.success("删除商品成功")
      this.getGoodList()
    },
    //跳转添加商品
    goAdd () {
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style scoped>
.el-table {
  margin-top: 15px;
  font-size: 12px;
}
.el-pagination {
  margin-top: 15px;
}
</style>